﻿using AME.Repositories;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace AME.Repository
{
    public class DiffLogRepository<T> : IDiffLogRepository<T> where T : class, new() 
    {
        protected readonly ISqlSugarClient _db;

        public DiffLogRepository(ISqlSugarClient db)
        {
            _db = db;
        }

        #region CRUD Operations

        // Add a new entity
        public async Task<bool> AddAsync(T entity)
        {
            return await _db.Insertable(entity).EnableDiffLogEvent().ExecuteCommandAsync() > 0;
        }

        // Add multiple entities
        public async Task<bool> AddRangeAsync(List<T> entities)
        {
            return await _db.Insertable(entities).EnableDiffLogEvent().ExecuteCommandAsync() > 0;
        }

        // Update an existing entity
        public async Task<bool> UpdateAsync(T entity)
        {
            return await _db.Updateable(entity).EnableDiffLogEvent().ExecuteCommandAsync() > 0;
        }

        // Delete an entity by primary key
        public async Task<bool> DeleteByIdAsync(object id)
        {
            return await _db.Deleteable<T>().In(id).EnableDiffLogEvent().ExecuteCommandAsync() > 0;
        }

        // Delete entities by condition
        public async Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression)
        {
            return await _db.Deleteable<T>().Where(whereExpression).EnableDiffLogEvent().ExecuteCommandAsync() > 0;
        }

        // Get an entity by primary key
        public async Task<T> GetByIdAsync(object id)
        {
            return await _db.Queryable<T>().InSingleAsync(id);
        }

        // Get entities by condition
        public async Task<List<T>> GetListAsync(Expression<Func<T, bool>> whereExpression)
        {
            return await _db.Queryable<T>().Where(whereExpression).ToListAsync();
        }

        // Get all entities
        public async Task<List<T>> GetAllAsync()
        {
            return await _db.Queryable<T>().ToListAsync();
        }

        // Check if an entity exists by condition
        public async Task<bool> AnyAsync(Expression<Func<T, bool>> whereExpression)
        {
            return await _db.Queryable<T>().Where(whereExpression).AnyAsync();
        }

        // Get a single entity by condition
        public async Task<T> GetSingleAsync(Expression<Func<T, bool>> whereExpression)
        {
            return await _db.Queryable<T>().Where(whereExpression).FirstAsync();
        }

        // Get a paged list of entities
        public async Task<(List<T> Data, int Total)> GetPagedListAsync(Expression<Func<T, bool>> whereExpression, int pageIndex, int pageSize)
        {
            RefAsync<int> totalCount = 0; // SqlSugar supports returning the total count as a reference parameter.
            var data = await _db.Queryable<T>()
                                .Where(whereExpression)
                                .ToPageListAsync(pageIndex, pageSize, totalCount);

            return (data, totalCount);
        }

        #endregion
    }
}
